/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package towerdefence.controller;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.media.opengl.GL;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.shapes.Box;
import towerdefence.model.Block;
import towerdefence.model.Builder;
import towerdefence.model.blocks.StyrofoamBlock;
import towerdefence.view.GLBlockRenderer;
import towerdefence.view.GLBuilderRenderer;
import towerdefence.view.GLCore;

/**
 *
 * @author Andreas
 */
public class BuilderController implements MouseMotionListener, MouseListener, Controller {

    private GLCore core;
    private Point start,  end;
    private Block block;
    private Builder builder;

    public BuilderController(GLCore core, Builder builder) {
        this.core = core;
        this.builder = builder;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        end = e.getPoint();
        Vector2f position = new Vector2f((start.x + end.x) / 2, (start.y + end.y) / 2);
        float width = Math.abs(end.x - start.x);
        float height = Math.abs(end.y - start.y);
        block = new StyrofoamBlock(position, width, height);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        start = e.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (block != null) {
            builder.getPlayer().addBlock(block);
            block = null;
            end = null;
            start = null;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void enable() {
        core.addController(this);
        core.addMouseListener(this);
        core.addMouseMotionListener(this);
    }

    @Override
    public void disable() {
        core.removeController(this);
        core.removeMouseListener(this);
        core.removeMouseMotionListener(this);
    }

    @Override
    public void draw(GL gl) {
        if (block != null) {
            GLBlockRenderer.drawBlock(gl, block);
        }
        GLBuilderRenderer.drawBuilder(gl, builder, Color.GREEN);
    }
}
